<?php
$dsn = "mysql:host=localhost;dbname=form_data;charset=utf8";
$user = 'root';
$pass = '123';
$pdo = new PDO($dsn,$user,$pass);
//连接数据库
$pdom = $pdo->query("select * from form_data");
//成功得到一个PDOStatement类 失败boolean(false)
$res = $pdom->fetchAll(PDO::FETCH_ASSOC);
//拿到数据信息
$pdom = $pdo->query("select count(*) number from form_data");
//成功得到一个PDOStatement类 失败boolean(false)
$number = $pdom->fetchAll(PDO::FETCH_ASSOC);
$during = [0,0,0];
$why = [0,0,0,0];
$type = [0,0,0,0];
$days = [0,0,0];
$time = [0,0,0];
$during = [0,0,0];
$state = [0,0];
$health = [0,0,0];
$reason = [0,0,0];
$damage = [0,0,0,0];

foreach($res as $index=>$value){
    //循环
    if($res[$index]['during']=='11:00~12:00'){
        //查找
        $during[0]++;
    }elseif($res[$index]['during']=='12:00~1:00'){
        $during[1]++;
    }elseif($res[$index]['during']=='1:00以后'){
        $during[2]++;
    }
    if(strpos($res[$index]['time'],'11点以后')!== false){
        //查找
        $time[0]++;
    }
    if(strpos($res[$index]['time'],'12点以后')!== false){
        $time[1]++;
    }
    if(strpos($res[$index]['time'],'1点以后')!== false){
        $time[2]++;
    }
    if(strpos($res[$index]['type'],'少于6小时')!== false){
        $type[0]++;
    }
    if(strpos($res[$index]['type'],'6-7小时')!== false){
        $type[1]++;
    }
    if(strpos($res[$index]['type'],'7-8小时')!== false){
        $type[2]++;
    }
    if(strpos($res[$index]['type'],'多于8小时')!== false){
        $type[3]++;
    }

    if(strpos($res[$index]['days'],'不熬夜')!== false){
        $days[0]++;
    }
    if(strpos($res[$index]['days'],'1-3天')!== false){
        $days[1]++;
    }
    if(strpos($res[$index]['days'],'每天')!== false){
        $days[2]++;
    }

    if(strpos($res[$index]['why'],'学习')!== false){
        $why[0]++;
    }
    if(strpos($res[$index]['why'],'打游戏')!== false){
        $why[1]++;
    }
    if(strpos($res[$index]['why'],'做课件')!== false){
        $why[2]++;
    }
    if(strpos($res[$index]['why'],'失眠')!== false){
        $why[3]++;
    }

    if(strpos($res[$index]['state'],'精力依旧旺盛')!== false){
        $state[0]++;
    }
    if(strpos($res[$index]['state'],'上课想睡觉')!== false){
        $state[1]++;
    }

    if(strpos($res[$index]['health'],'6小时')!== false){
        $health[0]++;
    }
    if(strpos($res[$index]['health'],'7小时')!== false){
        $health[1]++;
    }
    if(strpos($res[$index]['health'],'8小时')!== false){
        $health[2]++;
    }

    if(strpos($res[$index]['reason'],'学业上的需要')!== false){
        $reason[0]++;
    }
    if(strpos($res[$index]['reason'],'宿舍没有营造良好的休息环境')!== false){
        $reason[1]++;
    }
    if(strpos($res[$index]['reason'],'外界的噪音')!== false){
        $reason[2]++;
    }

    if(strpos($res[$index]['damage'],'头痛')!== false){
        $damage[0]++;
    }
    if(strpos($res[$index]['damage'],'腰酸背痛')!== false){
        $damage[1]++;
    }
    if(strpos($res[$index]['damage'],'精神不振')!== false){
        $damage[2]++;
    }
    if(strpos($res[$index]['damage'],'注意力不集中')!== false){
        $damage[3]++;
    }
    
    
}
$data = array ('during'=>$during,'why'=>$why,'type'=>$type,'time'=>$time,'days'=>$days,
'damage'=>$damage,'reason'=>$reason,'health'=>$health,'state'=>$state,'all'=>$number[0]['number']);
echo json_encode($data);

